package cn.hk.vaccination.controller;

import cn.hk.common.utils.DeptTreeUtil;
import cn.hk.dept.domain.vo.DeptVo;
import cn.hk.dept.service.AreaService;
import cn.hk.dept.service.DeptService;
import cn.hk.user.domain.UserInfo;
import cn.hk.user.service.UserInfoService;
import cn.hk.vaccination.domain.SubscribeInfo;
import cn.hk.vaccination.domain.VaccinesBasicsInfo;
import cn.hk.vaccination.domain.vo.CancelOderVo;
import cn.hk.vaccination.domain.vo.OrderRecorderVo;
import cn.hk.vaccination.domain.vo.VaccinationStockVo;
import cn.hk.vaccination.domain.vo.VaccinationVo;
import cn.hk.vaccination.service.*;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONUtil;
import com.ruoyi.common.core.domain.AjaxResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 预约控制层
 */


@RestController
@RequestMapping("/vaccinationservice")
@Api("预约接种服务")
public class VaccinationController {

    @Autowired
    private UserInfoService userInfoService;

    @Autowired
    private VaccinationService vaccinationService;

    @Autowired
    private VaccinesBasicsInfoService vaccinesBasicsInfoService;

    @Autowired
    private VaccinesPlanService vaccinesPlanService;

    @Autowired
    private SubscribeSettingService subscribeSettingService;

    @Autowired
    private AreaService areaService;

    @Autowired
    private DeptTreeUtil deptTreeUtil;

    @Autowired
    private SubscribeInfoService subscribeInfoService;

    @Autowired
    private DeptService deptService;


    /**
     * 获取预约接种列表
     *
     * @return
     */
    @ApiOperation("预约接种列表")
    @GetMapping("/getAppointmentVaccinationList/{userId}")
    public AjaxResult getAppointmentVaccinationList(@ApiParam("用户id")@PathVariable(value = "userId") Long userId) {
        //获取当前用户信息
        UserInfo curUser = vaccinationService.getUserById(userId);
        //获取疫苗列表
        List<VaccinationVo> vaccinationVoList = vaccinationService.getVaccinationVoList(curUser);
        return AjaxResult.success().put("item", vaccinationVoList);
    }

    /**
     * 获取疫苗详情
     *
     * @return
     */
    @ApiOperation("疫苗详情")
    @GetMapping("/getVaccinesDetail/{vid}")
    public AjaxResult getVaccinesDetail(@ApiParam("疫苗id")@PathVariable(value = "vid") Long vid) {
        VaccinesBasicsInfo vaccinesBasicsInfo = vaccinesBasicsInfoService.getVaccinesDetailById(vid);
        return AjaxResult.success().put("item", vaccinesBasicsInfo);
    }

    /**
     * 选择预约日期
     */
    @ApiOperation("预约日期")
    @GetMapping("/getOderDate/{deptId}")
    public AjaxResult getOderDate(@ApiParam("社区id")@PathVariable Long deptId) {
        Map<String, Object> SubscribeDetailMap = vaccinationService.getOderDateBYDeptId(deptId);
        return AjaxResult.success().put("item", SubscribeDetailMap);
    }

    /**
     * 查看社区疫苗库存
     *
     * @return
     */
    @ApiOperation("查看社区疫苗库存")
    @PostMapping("/getDeptVaccinesStock")
    public AjaxResult deptSelectedList(@RequestBody VaccinationStockVo vaccinationStockVo) {
        List<DeptVo> resultList = deptService.getDeptVaccinesStockList(vaccinationStockVo.getDeptId(), vaccinationStockVo.getVid());
        JSONArray areaTreesJsonArrayon = JSONUtil.parseArray(resultList);
        return AjaxResult.success().put("item", areaTreesJsonArrayon);
    }

    /**
     * 疫苗预约
     */
    @ApiOperation("疫苗预约")
    @PostMapping("/vaccineOder")
    public AjaxResult vaccineOder(@RequestBody SubscribeInfo subscribeInfo) {
        return subscribeInfoService.vaccineOder(subscribeInfo);
    }

    /**
     * 预约列表
     */
    @ApiOperation("预约列表")
    @GetMapping("/vaccineOderList/{userId}")
    public AjaxResult vaccineOderList(@ApiParam("用户id")@PathVariable("userId") Long userId){
        List<SubscribeInfo> subscribeInfoList =  subscribeInfoService.getVaccineOderList(userId);
        return AjaxResult.success().put("item",subscribeInfoList);
    }

    /**
     * 预约待办列表
     */
    @ApiOperation("待办列表")
    @GetMapping("/vaccineOderToDoList/{userId}")
    public AjaxResult vaccineOderToDoList(@ApiParam("用户id")@PathVariable("userId") Long userId){
        List<SubscribeInfo> vaccineOderToDoList =  subscribeInfoService.getVaccineOderToDoList(userId);
        return AjaxResult.success().put("item",vaccineOderToDoList);
    }

    /**
     * 儿童接种疫苗列表
     * @param userId
     * @return
     */
    @ApiOperation("儿童接种疫苗列表")
    @GetMapping("/vaccineList2Chr/{userId}")
    public AjaxResult vaccineList2Chr(@ApiParam("社区Id")@PathVariable("userId") Long userId){
        List<VaccinationVo> VaccinationVos =  vaccinesPlanService.getVaccineList2Chr(userId);
        return AjaxResult.success().put("item",VaccinationVos);
    }

    /**
     * 接种记录
     */
    @ApiOperation("接种记录")
    @GetMapping("/vaccineRecordList/{userId}")
    public AjaxResult vaccineRecordList(@ApiParam("用户id") @PathVariable("userId") Long userId){
        List<OrderRecorderVo> vaccineRecordList =  subscribeInfoService.getVaccineRecordList(userId);
        return AjaxResult.success().put("item",vaccineRecordList);
    }

    /**
     * 现场签到
     */
    @ApiOperation("现场签到")
    @PostMapping("/signIn")
    public AjaxResult signIn(@RequestBody SubscribeInfo subscribeInfo){
        return vaccinationService.sinIn(subscribeInfo);
    }

    /**
     * 取消预约
     */
    @ApiOperation("取消预约")
    @PostMapping ("/cancelOder")
    public AjaxResult cancelOder(@RequestBody CancelOderVo cancelOderVo){
        return vaccinationService.cancelOder(cancelOderVo);
    }
}
